<template>
  <teleport to="#message">
    <div class="alert message-info fixed-top w-50 mx-auto d-flex justify-content-between mt-2" :class="classObj" v-if="isVisible">
      <span>{{ message }}</span>
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close" @click.prevent="close"></button>
    </div>
  </teleport>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from 'vue';
import useDOMCreate from '../hooks/useDOMCreate';

export type MessageType = 'success' | 'error' | 'default';

export default defineComponent({
  name: 'Loader',
  props: {
    message: String,
    type: {
      type: String as PropType<MessageType>,
      default: 'default',
    },
  },
  emits: ['close-message'],
  setup(props, ctx) {
    useDOMCreate('message');

    const isVisible = ref(true);
    const classObj = {
      'alert-success': props.type === 'success',
      'alert-danger': props.type === 'error',
      'alert-primary': props.type === 'default',
    };

    const close = () => {
      isVisible.value = false;
      ctx.emit('close-message', true);
    };

    return { isVisible, close, classObj };
  },
});
</script>

<style lang="scss" scoped></style>
